package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.common.PackageUtils;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnItemClickListener;
import com.hundsun.yr.universal.library.view.itemview.SettingView;
import com.hundsun.yr.universal.library.view.itemview.SettingView.onSettingViewItemClickListener;
import com.hundsun.yr.universal.library.view.itemview.SettingViewItemData;
import com.hundsun.yr.universal.library.widget.SwitchButton;
import com.hundsun.yr.universal.p2p.HsApplication;
import com.hundsun.yr.universal.p2p.bean.BankAccountBeen;
import com.hundsun.yr.universal.p2p.bean.ManeBean;
import com.hundsun.yr.universal.p2p.bean.MineInvestProtocolBean;
import com.hundsun.yr.universal.p2p.bean.UpdatePayPwdBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.core.FingerprintCore;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.presenter.impl.SettingPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.MineSettingView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;


/**
 * @version V1.0
 * @Title: MineSettingActivity.java
 * @Description: 我的设置
 * @date 2015-12-4 下午2:17:04
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class MineSettingActivity extends BaseActivity implements MineSettingView, OnClickListener,
        onSettingViewItemClickListener, HSDialogOnItemClickListener {

    /**
     * 请求
     **/
    private RequestPresenter mPresenter = null;

    /**
     * 网络请求
     */
    private RequestPresenter mPresenter2 = null;

    /**
     * 当前版本号
     */
    private TextView tv_current_version = null;

    /**
     * 基础信息
     **/
//    private SettingView mBaseSettingView = null;
    /**
     * 扩展信息
     **/
    private SettingView mExpandSettingView = null;
    /**
     * 退出弹窗
     **/
    private HSDialog dialog = null;
    private String zwgt_data;

    private LinearLayout ll_userinfo_message = null;

    /**
     * 指纹swich_button
     */
    private SwitchButton switchb_setgesturepass = null;

    /**
     * 手势密码switch_button
     */
    private SwitchButton switch_set_gesture_password = null;

    /**
     * 指纹识别器
     */
    private FingerprintCore mFingerprintCore;

    /**
     * 指纹密码
     */
    private LinearLayout ll_finger_login;

    /**
     * 消息设置
     */
    private LinearLayout ll_message_setting;
    private boolean ifGesture;
    private String gesturelockResult;

    private int gestureOpen = 1;
    private int gestureClose = 2;
    private HSDialog paypwdDialog = null;
    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    protected int getContentViewLayoutID() {
        // TODO Auto-generated method stub
        return R.layout.act_setting;
    }

    /**
     * 接受eventBus的回调
     * @param eventCenter
     */
    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

        switch (eventCenter.getEventCode()) {
            case Constants.EVENT_REFRESH_DATA:
                //刷新登录接口
                mPresenter.initialized();
                break;
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
/*        //更具风险等级显示
        *//**
         * 0	未进行风险评估
         1	不通过
         10	保守型
         20	稳健型
         30	平衡型
         40	成长型
         50	进取型/激进型
         *//*
        String leverStatu = "";
        switch (sharedPreferences.getShareProf(UserInfo.USERLEVEL)) {
            case "0":
                leverStatu = getString(R.string.level0);
                break;
            case "1":
                leverStatu = getString(R.string.level1);
                break;
            case "10":
                leverStatu = getString(R.string.level10);
                break;
            case "20":
                leverStatu = getString(R.string.level20);
                break;
            case "30":
                leverStatu = getString(R.string.level30);
                break;
            case "40":
                leverStatu = getString(R.string.level40);
                break;
            case "50":
                leverStatu = getString(R.string.level50);
                break;
            default:
                leverStatu = getString(R.string.level0);
                break;
        }
        tv_risk_grade.setText(leverStatu);*/

        mPresenter.initialized();

        zwgt_data = BaseRequestTool2.getFormatedDateString();
        mPresenter.loadingData(TaskConstants.TASK_BANK_ACCOUNT, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_BANK_ACCOUNT, Constants.EVENT_BEGIN, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test)
                + TaskConstants.TASK_BANK_ACCOUNT, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
    }

    @Override
    protected boolean isBindEventBusHere() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        // TODO Auto-generated method stub
        setToolbarTitle(R.string.setting_space);
        mFingerprintCore = new FingerprintCore(this);
        switchb_setgesturepass = findView(R.id.switchb_setgesturepass);
        switch_set_gesture_password = findView(R.id.switch_set_gesture_password);
        ll_finger_login = findView(R.id.ll_finger_login);
        ll_message_setting=findView(R.id.ll_message_setting);
       ll_message_setting.setOnClickListener(this);
//         ll_risk_assessment = findView(R.id.ll_risk_assessment);
//        tv_risk_grade = findView(R.id.tv_risk_grade);
//        ll_risk_assessment.setOnClickListener(this);

        // TODO: 2017/6/30 先判断机子是否支持指纹识别
        if (mFingerprintCore.isSupport()){
            //判断用户是否有指纹识别
            switchb_setgesturepass.setVisibility(View.VISIBLE);
        } else {
            //机子没有指纹识别
            ll_finger_login.setVisibility(View.GONE);
        }

        // TODO: 2017/6/30 判断用户是否录入了指纹
        if (!mFingerprintCore.isHasEnrolledFingerprints()) {
            ll_finger_login.setVisibility(View.GONE);
        }

        // TODO: 2017/6/30 判断用户是否开启了指纹识别
        if (mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_FINGERPRINT)){
            switchb_setgesturepass.setChecked(true);
        } else {
            switchb_setgesturepass.setChecked(false);
        }

        switchb_setgesturepass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //保存状态
                mConfigSP.putBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_FINGERPRINT,isChecked);
            }
        });

        /**
         * 判断用户开启手势识别的状态
         */
        gesturelockResult = mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE);
        ifGesture = mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE);

        if (ifGesture) {
            switch_set_gesture_password.setChecked(true);
        } else {
            switch_set_gesture_password.setChecked(false);
        }

        switch_set_gesture_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    readyGoForResult(GestureLockActivity.class,1);
                } else {
                    Bundle data = new Bundle();
                    data.putInt("openflag", gestureClose);
                    readyGoForResult(GestureLockVerify2Activity.class,1,data);
                }
            }
        });

        mExpandSettingView = findView(R.id.setting_expand);
        tv_current_version = findView(R.id.tv_current_version);
        tv_current_version.setText(getString(R.string.current_version, PackageUtils.getVersionName(mContext)));
        mExpandSettingView.setOnSettingViewItemClickListener(this);

        mPresenter = new SettingPresenterImpl(this, this);
        mPresenter.initialized();
        mPresenter2 = new InitializePresenterImpl(mContext,this);

    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }


    @Override
    protected void onNetworkConnected(NetType type) {
        // TODO Auto-generated method stub
    }

    @Override
    protected void onNetworkDisConnected() {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void setAdapter(List<SettingViewItemData> mBaseData,
                           List<SettingViewItemData> mExpandData) {
        // TODO Auto-generated method stub
/*        mBaseSettingView.removeAllViews();
        mBaseSettingView.setAdapter(mBaseData);*/
        mExpandSettingView.removeAllViews();
        mExpandSettingView.setAdapter(mExpandData);
    }

    @Override
    public void modifyBaseSubTitle(String subTitle, int index) {
        // TODO Auto-generated method stub

    }

    @Override
    public void modifyExpandSubTitle(String subTitle, int index) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_login:
                dialog = new HSDialog(null, getString(R.string.sure_loginout), getString(R.string.cancel),
                        getString(R.string.sure), this, this).setCancelable(false);//.setOnDismissListener(this);
                dialog.show();
                break;
            case R.id.ll_message_setting:
                readyGo(MessageSettingActivity.class);
                break;
//            case R.id.ll_risk_assessment:
//                if (!NetUtils.isNetworkConnected(mContext)){
//                    showProgressToDismiss();
//                    return;
//                }
//
//                //level  0 和 1可以点击进入风险评估页面的
//                if (sharedPreferences.contains(mContext,UserInfo.IF_ACCOUNT) && TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT), "YES")){
//                    if (!sharedPreferences.getBoolean(UserInfo.OLDUSER) && (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"0") || TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"1"))){
//                        zwgt_data = BaseRequestTool2.getFormatedDateString();
//                        HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
//                                TaskConstants.TASK_RISKESTIMATE, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
//                        HeaderMap headerMap = new HeaderMap();
//                        headerMap.setMap(hearder);
//                        Bundle bundle = new Bundle();
//                        bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.risk_estimate));
//                        bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_RISKESTIMATE);
//                        bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
//                        bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
//                        bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
//                        readyGo(WebRiskActivity.class, bundle);
//                    } else if ((sharedPreferences.getBoolean(UserInfo.OLDUSER) && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"10") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"20") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"30") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"40") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"50"))){
//                        zwgt_data = BaseRequestTool2.getFormatedDateString();
//                        HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
//                                TaskConstants.TASK_RISKESTIMATE, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
//                        HeaderMap headerMap = new HeaderMap();
//                        headerMap.setMap(hearder);
//                        Bundle bundle = new Bundle();
//                        bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.risk_estimate));
//                        bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_RISKESTIMATE);
//                        bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
//                        bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
//                        bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
//                        readyGo(WebRiskActivity.class, bundle);
//                    }
//                } else {
//                    //未开户
//                    showToast(getString(R.string.open_account));
//                    readyGo(OpenAccountBankActivity.class);
//                }
//                break;
        }
    }

    @Override
    public void onItemClick(int index) {
        // TODO Auto-generated method stub

        if (isFastDoubleClick()){
            return;
        }

        switch (index) {
            case 0://查看开户
                if (!NetUtils.isNetworkConnected(mContext)){
                    showProgressToDismiss();
                    return;
                }

                checkOpenAccountStatus();//验证开户情况
                break;
            case 1://登录手机号
                readyGo(SetUpdatePhoneActivity.class);
                break;

            case 2://登录密码
                //跳到管理登录密码
                readyGo(PasswordMangerActivity.class);
                break;

            case 3://交易密码
                setPayPwd();
                break;

//            case 4://跳转修改江西银行预留手机号
//                readyGo(UpdateJXReservedPhoneActivity.class);
//                break;

           /* case 4://风险评估
                if (!NetUtils.isNetworkConnected(mContext)){
                    showProgressToDismiss();
                    return;
                }

                //level  0 和 1可以点击进入风险评估页面的
                if (sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT) != null && TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT), "YES")){
                    if (!sharedPreferences.getBoolean(UserInfo.OLDUSER) && (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"0") || TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"1"))){
                            zwgt_data = BaseRequestTool2.getFormatedDateString();
                            HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                                    TaskConstants.TASK_RISKESTIMATE, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                            HeaderMap headerMap = new HeaderMap();
                            headerMap.setMap(hearder);
                            Bundle bundle = new Bundle();
                            bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.risk_estimate));
                            bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_RISKESTIMATE);
                            bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
                            bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
                            bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                            readyGo(WebRiskActivity.class, bundle);
                    } else if ((sharedPreferences.getBoolean(UserInfo.OLDUSER) && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"10") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"20") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"30") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"40") && !TextUtils.equals(sharedPreferences.getShareProf(UserInfo.USERLEVEL),"50"))){
                        zwgt_data = BaseRequestTool2.getFormatedDateString();
                        HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                                TaskConstants.TASK_RISKESTIMATE, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                        HeaderMap headerMap = new HeaderMap();
                        headerMap.setMap(hearder);
                        Bundle bundle = new Bundle();
                        bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.risk_estimate));
                        bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_RISKESTIMATE);
                        bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
                        bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
                        bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                        readyGo(WebRiskActivity.class, bundle);
                    }
                } else {
                    //未开户
                    showToast(getString(R.string.open_account));
                    readyGo(OpenAccountBankActivity.class);
                }
                break;

            case 5://手势密码
                readyGo(SetGesturePassActivity.class);
                break;

            case 6://跳转修改江西银行预留手机号
                readyGo(UpdateJXReservedPhoneActivity.class);
                break;*/


        }
    }

    /**
     * 设置交易密码
     */
    public void setPayPwd() {
        if (!TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT), "YES")) {
            showToast(getString(R.string.open_account));
            readyGo(OpenAccountBankActivity.class);
            return;
        }

		if(!TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_BINDCARD), "YES")){
			showToast(getString(R.string.bind_card_statu));
//            readyGo(BindCardActivity.class);
            if (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_PAYPWD), "YES")){
                bindCar();
            }else {
                //去设置交易密码
                paypwdDialog = new HSDialog(getString(R.string.tips), getString(R.string.tips_nopaypwd),
                        getString(R.string.cancel), getString(R.string.tips_go_set), mContext, this).setCancelable(false);
                paypwdDialog.show();
            }

			return;
		}
        /**
         *  去设置交易密码
         */
        if (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_PAYPWD), "NO")){
            String  zwgt_data = BaseRequestTool2.getFormatedDateString();
            JSONObject map1 = new JSONObject();
            JSONObject jsonParams = CommTool.getJSONParams(map1, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
            mPresenter.loadingData(TaskConstants.TASK_SETBANKPWD, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, Constants.EVENT_DATA,
                    jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, sharedPreferences
                            .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
            return;
        }

      //去重置交易密码
        String title = "";
        if (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_PAYPWD), "YES")) {
            title = getString(R.string.update2_trading_pass);
            Bundle bundle = new Bundle();
            bundle.putString("title",title);
            if (sharedPreferences.getShareProf(UserInfo.BANKMOBILE)!= null){
                bundle.putString("phone",sharedPreferences.getShareProf(UserInfo.BANKMOBILE));
            }
            String zwgt_data = BaseRequestTool2.getFormatedDateString();
            JSONObject map = new JSONObject();

            JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
            mPresenter2.loadingData(TaskConstants.TASK_RETBANKRETPWD, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, Constants.EVENT_DATA,
                    jsonParams,BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, sharedPreferences
                            .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
        }

    }

    @Override
    public void initUserInfo(String userlogo, String username, String certno) {
    }

    @Override
    public void onItemClick(Object o, int position) {
        // 安全退出的时候把token什么的都弄没掉，islogin变成false
        if ((HSDialog)o==dialog&&position != -1) {
            sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
            sharedPreferences.clear(mContext);

            BaseAppManager.getInstance().clearToTop();
            HsApplication.goGesture = true;
            HsApplication.isSrcreenOn = false;
            Bundle bundle = new Bundle();
            bundle.putString(Extras.NAVIGATE_TAG, "HOME");
            readyGoThenKill(MainActivity.class, bundle);
//            EventBus.getDefault().post(new EventCenter(Constants.EVENT_REFRESH_DATA));//登陆成功通知某些页面进行数据刷新
            //去设置交易密码
        }else if ((HSDialog) o == paypwdDialog) {
            if (position != -1) {
                String  zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map1 = new JSONObject();
                JSONObject jsonParams = CommTool.getJSONParams(map1, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_SETBANKPWD, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, Constants.EVENT_DATA,
                        jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, sharedPreferences
                                .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
            }
        }

    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name){
            //设置交易密码回调
            case TaskConstants.TASK_SETBANKPWD:
                if (data == null){
                    return;
                }
                Log.e("rebankpwd",data.toString());
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                       "setbankpwd", sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap2 = new HeaderMap();
                headerMap2.setMap(hearder);
                Bundle bundler = new Bundle();
                bundler.putString(BaseWebActivity.BUNDLE_KEY_URL,data.toString());
                bundler.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap2);
                bundler.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGoThenKill(WebOpenAccountActivity.class, bundler);
                if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null){
                    BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                }
                break;
                //重置交易密码回调
            case TaskConstants.TASK_RETBANKRETPWD:
                if (data == null){
                    return;
                }
                Log.e("rebankpwd",data.toString());
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder0 = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                       "rebankpwd", sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap0 = new HeaderMap();
                headerMap0.setMap(hearder0);
                Bundle bundler0 = new Bundle();
                bundler0.putString(BaseWebActivity.BUNDLE_KEY_URL,data.toString());
                bundler0.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap0);
                bundler0.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGoThenKill(WebOpenAccountActivity.class, bundler0);
                if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null){
                    BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                }
                break;
            case TaskConstants.TASK_CARDBIND:
                Gson gson =new Gson();
                ManeBean maneBean = gson.fromJson(String.valueOf(data), ManeBean.class);
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder1 = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                        "bindcardpagerst", sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap1 = new HeaderMap();
                headerMap1.setMap(hearder1);
                Bundle bundle1 = new Bundle();
                bundle1.putString(BaseWebActivity.BUNDLE_KEY_URL, maneBean.getHtml());
                bundle1.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap1);
                bundle1.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGo(WebOpenAccountActivity.class, bundle1);
                break;

            case TaskConstants.TASK_BANK_ACCOUNT:
                //查询托管账户信息，更新ui
                BankAccountBeen bankAccountBeen = (BankAccountBeen) data;
                //保存余额信息
//                sharedPreferences.putShareProf(UserInfo.CAN_USE_MONEY, bankAccountBeen.availBal);

                //对江西银行卡信息进行显示
                if (!bankAccountBeen.depository_bank_no.isEmpty()) {//如果返回有银行卡信息，则将银行卡信息保存并且显示银行卡
                    //将获得的银行卡信息保存
                    sharedPreferences.putShareProf(UserInfo.JX_ACCOUNT_NO, bankAccountBeen.depository_bank_no);
                }

                //如果获取了用户的银行卡信息就将其绑定
                /**存储开户信息**/
                if (bankAccountBeen.depository_bank_no != null && !bankAccountBeen.depository_bank_no.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.JIANGXI_BANKCARD, bankAccountBeen.depository_bank_no);
                    sharedPreferences.putShareProf(UserInfo.IF_ACCOUNT, "YES");
                } else {
                    sharedPreferences.putShareProf(UserInfo.IF_ACCOUNT, "NO");
                }

                /**存储绑卡信息**/
                if (bankAccountBeen.bankCard != null && !bankAccountBeen.bankCard.isEmpty() && bankAccountBeen.bankName != null && !bankAccountBeen.bankName.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.BANKCARD, bankAccountBeen.bankCard);
                    sharedPreferences.putShareProf(UserInfo.BANKCARDNAME, bankAccountBeen.bankName);

                    sharedPreferences.putShareProf(UserInfo.IF_BINDCARD, "YES");
                } else {
                    sharedPreferences.putShareProf(UserInfo.IF_BINDCARD, "NO");
                }

                if (bankAccountBeen.true_name != null && !bankAccountBeen.true_name.isEmpty()){
                    sharedPreferences.putShareProf(UserInfo.REAL_USER_NAME, bankAccountBeen.true_name);
                }

                /**
                 * 绑定身份证
                 */
                if (bankAccountBeen.cert_no != null && !bankAccountBeen.cert_no.isEmpty()){
                    sharedPreferences.putShareProf(UserInfo.CERT_NO, bankAccountBeen.cert_no);
                }

                /**保存登陆手机号**/
                //保存手机号码
                if (bankAccountBeen.phone != null && !bankAccountBeen.phone.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.MOBILE, bankAccountBeen.phone);
                }

/*            *//**保存用户交易密码设置状态**/
                if (bankAccountBeen.set_paypwd_flag != null && !bankAccountBeen.set_paypwd_flag.isEmpty()) {
                    if (TextUtils.equals(bankAccountBeen.set_paypwd_flag, "1")) {
                        //已经设置交易密码
                        sharedPreferences.putShareProf(UserInfo.IF_PAYPWD, "YES");
                    } else {
                        sharedPreferences.putShareProf(UserInfo.IF_PAYPWD, "NO");
                    }
                } else {
                    sharedPreferences.putShareProf(UserInfo.IF_PAYPWD, "NO");
                }

                /**保存可用余额**/
                if (bankAccountBeen.availBal != null && !bankAccountBeen.availBal.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.CAN_USE_MONEY, bankAccountBeen.availBal);
                }

                //保存用户vip等级
                if (bankAccountBeen.vip != null && !bankAccountBeen.vip.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.VIPLEVEL, bankAccountBeen.vip);
                }

                break;


        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        // TODO Auto-generated method stub

    }

    /**
     * 根据开户情况进行开户，或者展示开户信息
     */
    public void checkOpenAccountStatus(){
        if (TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT),"YES")){
            String zwgt_data = BaseRequestTool2.getFormatedDateString();
            HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                    TaskConstants.TASK_ACTINFO, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
            HeaderMap headerMap = new HeaderMap();
            headerMap.setMap(hearder);

            Bundle bundle = new Bundle();
            bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.modify_open_account));
//                bundle7.putString(WebProtocolActivity.PROTOCOL_CODE, EnumProtocol.findCode(EnumProtocol.HELP));
            bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_ACTINFO);
            bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER,headerMap);
            bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
            bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
            readyGo(WebActivity.class, bundle);
        } else{
            //未开户
            readyGo(OpenAccountBankActivity.class);
        }
    }

    private void showProgressToDismiss() {
        showProgress("");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                closeProgress();
            }
        }, 2000);
    }
    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        Intent intent = new Intent(MineSettingActivity.this,MineSettingActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void bindCar(){
        //绑定银行卡
        String zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();
        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
        mPresenter.loadingData(TaskConstants.TASK_CARDBIND, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_CARDBIND,
                Constants.EVENT_DATA,jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, getString(R.string.api_user_test) + TaskConstants.TASK_CARDBIND,
                        sharedPreferences.getShareProf(UserInfo.USER_NO),
                        sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);

    }



}
